﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

namespace DataSources
{
    public class BlockRequestEventArgs : EventArgs
    {
        public string hash;
        public string text;

        public BlockRequestEventArgs(string hash, string text)
        { 
            this.hash = hash;
            this.text = text;
        }
    }

    public struct SpreadsheetInfo // TODO: remove and use only SpreadsheetEntry
    {
        public string title;
        public string feedUri;
        public SpreadsheetEntry entry;

        public SpreadsheetInfo(string title, string feedUri, SpreadsheetEntry entry)
        {
            this.title = title;
            this.feedUri = feedUri;
            this.entry = entry;
        }

        public static SpreadsheetInfo Empty = new SpreadsheetInfo();
    }

    public class GoogleSpreadsheets : IDataSource
    {
        protected SpreadsheetsService service = null;
        protected Hashtable spreadsheets = null;
        protected bool isAuthOK = false;

        public GoogleSpreadsheets(string login, string password)
        {
            service = new SpreadsheetsService("Spreadsheet-GData-CodeBrigade");
            service.setUserCredentials(login, password);

            try
            {
                RequestData();
                isAuthOK = true;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
                isAuthOK = false;
            }
        }

        private void RequestData()
        {
            // load projects (spreadsheets)
            SpreadsheetQuery querySpreadsheet = new SpreadsheetQuery();
            SpreadsheetFeed feedSpreadsheet = this.service.Query(querySpreadsheet);
            this.spreadsheets = new Hashtable();
            AtomEntryCollection spreadsheetEntries = feedSpreadsheet.Entries;
            for (int i = 0; i < spreadsheetEntries.Count; i++)
            {
                SpreadsheetEntry spreadsheetEntry = spreadsheetEntries[i] as SpreadsheetEntry;

                // Get the worksheets feed URI
                AtomLink worksheetsLink = spreadsheetEntry.Links.FindService(
                    GDataSpreadsheetsNameTable.WorksheetRel,
                    AtomLink.ATOM_TYPE
                );

                // Create an item to store the title and the spreadsheet's feed URI
                this.spreadsheets.Add(
                    spreadsheetEntries[i].Id.AbsoluteUri,
                    new SpreadsheetInfo(spreadsheetEntry.Title.Text, worksheetsLink.HRef.Content, spreadsheetEntry)
                );
            }
        }

        public bool IsAuthOK()
        {
            return isAuthOK;
        }

        public string GetOwnerHash()
        {
            return string.Empty;
        }

        public void ReloadData()
        {
            RequestData();
        }

        public SpreadsheetInfo GetEntryInfo(string entryHash)
        {
            if (spreadsheets.ContainsKey(entryHash))
                return (SpreadsheetInfo)spreadsheets[entryHash];
            return SpreadsheetInfo.Empty;
        }
    }
}
